<template>
	<scroll-view scroll-y class="scrollPage">
		<cu-custom bgImage="https://image.weilanwl.com/color2.0/plugin/sylb2244.jpg" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">提交内容</block>
		</cu-custom>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title margin  cuIcon-activity line-green">标题</view>
				<input placeholder="例如:物17.5" v-model="title"></input>
			</view>
			<view class="cu-form-group">
				<view class="title margin cuIcon-selection line-red">To对象</view>
				<input placeholder="例如:xxx" v-model="sendto"></input>
			</view>

			<view class="bg-white">
				<view class="margin-lg line-blue text-center cuIcon-news  text-bold">想说的话</view>
				<view class="cu-form-group margin-top">
					<textarea maxlength="-1" placeholder="请在此处填写哟O(∩_∩)O~" auto-height focus v-model="saycontent"></textarea>
				</view>
			</view>

			<!-- 	<view class="cu-bar bg-white ">
			<view class="action cuIcon-album line-grey">
				讲评图片上传
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				
				<view class="grid col-4 grid-square flex-sub">
				 	<view class="bg-img" @tap="ViewImage" :data-url="image" v-if="image">
				 	 <image :src="image" mode="aspectFill" v-model ='image'></image>
				 		<view class="cu-tag bg-red" @tap.stop="DelImg">
				 			<text class='cuIcon-close'></text>
				 		</view>
				 	</view>
				 	<view class="solids" @tap="ChooseImage">
				 		<text class='cuIcon-cameraadd'></text>
				 	</view>
				 </view>
			</view>
		</view>
		 -->



			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/9
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
						<image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<9">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>










			<view class="text-center bg-white padding-lg margin-top-sm">
				<button class="cu-btn round lines-green" @click="submit">提交</button>
			</view>



		</form>


		<view class="cu-modal" :class="modalName=='showprocess'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">正在上传第{{indexofupload}}个文件</view>
					<view class="action" @tap="hideCommentWindow">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					<view class="cu-progress round sm striped active">
						<view class="bg-green" :style="'width:'+_self.process+'%'"></view>
					</view>
				</view>
				<!-- <view class="cu-bar bg-white">
				<view class="action margin-0 flex-sub  solid-left" @tap="sendToComment">确定</view>
			</view> -->
			</view>
		</view>

	</scroll-view>
</template>

<script>
	var _self;
	// import NAUIcard from '@/components/NAUI-card/NAUI-card.vue';
	// import cpimg from "@/components/cpimg/cpimg.vue"
	export default {
		data() {
			return {
				image: '',
				title: '',
				sendto: '',
				imagesList: [],
				saycontent: '',
				images: '',
				imgList: [],
				modalName: '',
				indexofupload: 0,
				process: '',
				channel:''
			}
		},
		// 注册组件
		// components: {NAUIcard},
		onLoad(options) {
			_self = this;
			this.channel = options.channel;
		},
		methods: {
			
			
			// 添加图片
			addPhoto() {
				let that = this;
				that.$refs.cpimg._changImg()
			},
			cpimgOk(file) {
				if (_self.imgList.length != 0) {
					_self.imgList = _self.imgList.concat(file);
					console.log(_self.imgList);
				
				} else {
					_self.imgList = file;
					console.log("------res.tempFilePaths---------");
					console.log(_self.imgList);
					console.log("-------res.tempFilePaths--------");
				}
				console.log(file);
			},
			cpimgErr(e) {
				console.log(e);
			},

			// showCommentWindow(e) {
			// 	this.modalName = e.currentTarget.dataset.target
			// },
			hideCommentWindow(e) {
				this.modalName = null
			},
			submit() {
				
					if(!this.sendto){
						uni.showToast({
							icon:"none",
							title:"您还没有填写对象哟～"
						});
						return;
					}
					if(!this.saycontent){
						uni.showToast({
						
							title:"您还没有填写想说的内容哟"
						});
						return;
					}
					_self.postToWeekend();

			},
			myswitch(e) {
				this.teacher_here = !this.teacher_here;
			},
			ChooseImage() {
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
							console.log(this.imgList);

						} else {
							this.imgList = res.tempFilePaths
							console.log("------res.tempFilePaths---------");
							console.log(this.imgList);
							console.log("-------res.tempFilePaths--------");
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},

			postToWeekend(){
				
				if(_self.imgList){
					this.uploadImage();
				}else{
					this.postData();
				}
			
			},



		uploadImage(){
				this.modalName = 'showprocess';
				for (let index in _self.imgList) {
					_self.indexofupload = parseInt(index)+1;
					const uploadTask = uni.uploadFile({
						url: this.$baseUrl + 'api/common/upload',
						filePath: _self.imgList[index],
						name: 'file',
						formData: {
							'token': uni.getStorageSync('token')
						},
						success: (res) => {
							var newimg = JSON.parse(res.data);
							_self.imagesList[index] = newimg.data.url;
							// this.postToWeekend();
							console.log(_self.imagesList[index]);
							if(_self.indexofupload ==_self.imgList.length){
								_self.images = _self.imagesList.join(',');
								console.log(_self.images);
								_self.postData();
							}
						
						}
					});
					uploadTask.onProgressUpdate((res) => {
						_self.process = res.progress;
					});

				}
				_self.modalName = '';
				_self.process = '';
				uni.showToast({
					title: "文件上传完毕,共" + _self.indexofupload + "个文件"
				});
				
				
				// _self.postData();
			},
		postData(){
				this.$http.post('addons/cms/api/index',{
					channel_id: _self.channel,
					sendto: _self.sendto,
					content: _self.saycontent,
					title: _self.title,
					images: _self.images
				})
				.then(res => {
					console.log(res);
					if (res.data.code == 1) {
						uni.redirectTo({
							url: './list?channel='+_self.channel
			
						});
					}
				}).catch(err => {
					console.log(err.data);
				});
		}
		}
	};
</script>





<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	#editor {
		width: 100%;
		height: 300px;
		background-color: #FFFFFF;
	}
</style>
